import $api from "@/api/index.js"
import $store from "@/store/index.js"
export default {
	state: {
		pageActive: false,
		haveOperItem: false,
		activeIndex: 0,
		tabList: [{
			title: '全部',
			id: 0,
		}, {
			title: '可服务',
			id: 1,
		}, {
			title: '服务中',
			id: 2
		}, {
			title: '可预约',
			id: 3
		}],
		serviceFilterList: [{
			title: '可服务',
			id: 1,
		}, {
			title: '服务中',
			id: 2
		}, {
			title: '可预约',
			id: 3
		}],
		sexList: [{
			id: -1,
			title: '不限'
		}, {
			id: 0,
			title: '男'
		}, {
			id: 1,
			title: '女'
		}],
		ageRange: [{
			id: 0,
			title: '80后'
		}, {
			id: 1,
			title: '85后'
		}, {
			id: 2,
			title: '90后'
		}, {
			id: 3,
			title: '95后'
		}],
		serviceTypeList: [{
			id: 0,
			title: '不限'
		}, {
			id: 2,
			title: '上门'
		}, {
			id: 1,
			title: '到店'
		}],
		sortList: [{
			id: 0,
			title: '默认排序'
		}, {
			id: 1,
			title: '人气推荐'
		}, {
			id: 2,
			title: '距离最近'
		}, {
			id: 3,
			title: '最新加入'
		}, {
			id: 4,
			title: '接单最多'
		}, {
			id: 5,
			title: '评分最高'
		}, {
			id: 6,
			title: '我收藏的'
		}],
		careerList: [],
		careerIndex: 0,
		sortIndex: 0,
		cityList: [],
		serviceCateList: [],
		checkCareerIndex: 0,
		check: {
			sex: -1,
			cate_id: 0,
			recommend: 0,
			free_fare: 0,
			service_type: 0, // 1到店，2上门
			store_name: '',
			serviceFilter: '',
			ageRange: [18, 55],
			ageNum: 0,
			distance: 10,
			serviceTimeRange: ''
		},
		param: {
			page: 1,
			city_id: 0,
			ser_id: 0,
			coach_name: '',
			type: 0
		},
		list: {
			data: [],
			last_page: 1,
			current_page: 1
		},
		order_coach: {}
	},
	mutations: {
		async updateTechnicianItem(state, item) {
			let {
				key,
				val
			} = item
			state[key] = val
		}
	},
	actions: {
		async getServiceCate({
			commit,
			state
		}, param) {
			let d = await $api.service.serviceCateList()
			commit('updateTechnicianItem', {
				key: 'serviceCateList',
				val: d
			})
		},
		// 获取职业
		async getIndustryType({
			commit,
			state
		}, param) {
			let d = await $api.service.industryTypeSelect()
			// d.unshift({title: '全部',id: 0})
			commit('updateTechnicianItem', {
				key: 'careerList',
				val: d
			})
		},
		async getCityList({
			commit,
			state
		}, param) {
			let {
				lat,
				lng,
				change = 0,
			} = param
			let d = await $api.base.getCity({
				lat,
				lng
			})
			let {
				city_id: choose_city_id = 0
			} = state.param
			let ind = d.findIndex(v => {
				return (change || !choose_city_id) ? v.is_select : v.id == choose_city_id
			})
			let is_huawei_app = 0
			// #ifdef APP-PLUS
			if ($store.state.config.configInfo.app_model_type == 1 && !param.lat && !param.lng) {
				is_huawei_app = 1
				ind = 0
			}
			// #endif
			commit('updateTechnicianItem', {
				key: 'cityList',
				val: d
			})
			let city_id = ind == -1 ? 0 : d[ind].id

			let val = Object.assign({}, state.param, {
				city_id
			})
			commit('updateTechnicianItem', {
				key: 'param',
				val
			})
			let city_info = ind == -1 ? {} : d[ind]
			$store.commit('updateUserItem', {
				key: 'city_info',
				val: city_info
			})
			// location不能删除
			let huawei_loca = {}
			// #ifdef APP-PLUS
			if (is_huawei_app) {
				huawei_loca = {
					lat: city_info.lat,
					lng: city_info.lng,
				}
			}
			// #endif
			let location = Object.assign({}, $store.state.user.location, {
				id: city_id
			}, huawei_loca)
			$store.commit('updateUserItem', {
				key: 'location',
				val: location
			})
		},
		async getServiceCoachList({
			commit,
			state
		}, param) {
			let {
				coach_format = 1
			} = $store.state.config.configInfo
			let methodModel = coach_format == 2 ? 'typeServiceCoachList' : 'serviceCoachList'
			let d = await $api.service[methodModel](param)
			let oldList = state.list;
			let newList = d;
			let list = {}
			if (param.page == 1) {
				list = newList;
			} else {
				newList.data = oldList.data.concat(newList.data)
				list = newList;
			}
			commit('updateTechnicianItem', {
				key: 'list',
				val: list
			})
		}
	},
}